package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.addressDao;
import org.netEase.entity.address;
import org.netEase.util.BaseDao;

public class addressDaoimpl extends BaseDao implements addressDao {

	@Override
	public List<address> getlistbyhid(int hid) {
		List<address> list = new ArrayList<>();
		String sql = "select * from dizhi where hid = ?";
		ResultSet rs = super.executeQuery(sql, hid);
		try {
			while (rs.next()) {
				address address = new address();
				address.setAddress1(rs.getString("address1"));
				address.setAddress2(rs.getString("address2"));
				address.setAddress3(rs.getString("address3"));
				address.setDzid(rs.getInt("dzid"));
				address.setHid(rs.getInt("hid"));
				address.setPhone(rs.getString("phone"));
				address.setShname(rs.getString("shname"));
				address.setXiangxi(rs.getString("xiangxi"));
				
				list.add(address);
			}
		} catch (SQLException e) {
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public address getbydzid(int dzid, int hid) {
		address address = null;
		String sql = "select * from dizhi where dzid = ? and hid = ?";
		ResultSet rs = super.executeQuery(sql, dzid,hid);
		try {
			while (rs.next()) {
				address = new address();
				address.setAddress1(rs.getString("address1"));
				address.setAddress2(rs.getString("address2"));
				address.setAddress3(rs.getString("address3"));
				address.setDzid(rs.getInt("dzid"));
				address.setHid(rs.getInt("hid"));
				address.setPhone(rs.getString("phone"));
				address.setShname(rs.getString("shname"));
				address.setXiangxi(rs.getString("xiangxi"));
				
			}
		} catch (SQLException e) {
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return address;
	}

	@Override
	public int add(address address) {
		String sql = "insert into dizhi(hid,shname,address1,address2,address3,xiangxi,phone)values(?,?,?,?,?,?,?)";
		return super.executeUpdate(sql, address.getHid(),address.getShname(),address.getAddress1(),address.getAddress2(),address.getAddress3(),address.getXiangxi(),address.getPhone());
	}

	@Override
	public int updadd(address address) {
		String sql = "update dizhi set shname = ?,address1 = ?,address2 = ?,address3 = ?,xiangxi=?,phone=? where dzid = ?";
		return super.executeUpdate(sql, address.getShname(),address.getAddress1(),address.getAddress2(),address.getAddress3(),address.getXiangxi(),address.getPhone(),address.getDzid());
	}

	@Override
	public address getlistbydzid(int dzid) {
		address address = null;
		String sql = "select * from dizhi where dzid = ?";
		ResultSet rs = super.executeQuery(sql, dzid);
		try {
			while (rs.next()) {
				address = new address();
				address.setAddress1(rs.getString("address1"));
				address.setAddress2(rs.getString("address2"));
				address.setAddress3(rs.getString("address3"));
				address.setDzid(rs.getInt("dzid"));
				address.setHid(rs.getInt("hid"));
				address.setPhone(rs.getString("phone"));
				address.setShname(rs.getString("shname"));
				address.setXiangxi(rs.getString("xiangxi"));
				
			}
		} catch (SQLException e) {
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return address;
	}

}
